Array = {
	["0-1"] = {"root-提示",{505,751,627,791},"0|0|0x009688,18|-4|0x009688,31|-4|0x009688,26|1|0x009688,22|5|0x009688,9|7|0xd9e6e5,-1|5|0x039789,-25|6|0x56b6ad,-37|3|0x05988a",95,0,0,0},
	["8-0-0-0"]={"训练-城堡界面",{519,528,588,591},"0|0|0xcd7d66,0|0|0xcd7d66,1|1|0xbc393e,2|3|0x8f553e,5|3|0x6e4135,6|4|0x562e28,3|5|0x5a382b,1|6|0x653423,5|7|0x512e25",95,0,0,0},
	["8-0-0-1"]={"邮件-城堡界面",{411,1172,594,1272},"0|0|0xce4e28,2|0|0xb65136,7|1|0xa1321e,-2|6|0xb5452c,-3|2|0x913424,14|-6|0xc1b174,8|0|0xde643d,2|6|0xba5038,0|9|0xa43923,7|7|0x8b3426,1|-3|0xbf492c,-4|2|0xb7472c,0|7|0xa54531",95,0,0,0},
	["8-0-0-1"]={"训练-城堡界面",{504,518,639,633},"0|0|0xcccdcd,0|2|0xe5e6e6,-2|5|0xfbfbfb,-9|4|0x838485,-10|1|0xcecfcf,-18|2|0x282a2a,-16|6|0xaaabac,-16|11|0xababab",95,0,0,0},
	["8-0-0-2"]={"训练-城堡界面",{594,513,707,625},"0|0|0xf0f0f1-0x111111,-2|5|0xf3f3f3-0x111111,-16|-3|0xb5b6b6-0x111111,-15|6|0xaaabab-0x111111,-20|7|0xeaeaea-0x111111,-30|5|0xcbcac9-0x111111",95,0,0,0},
	["8-0-0-3"]={"训练-按钮",{386,1156,668,1259},"0|0|0xe0dfa0,0|6|0xdfdc9d,-12|9|0x034d32,-36|4|0x086646,-47|2|0x066244,0|10|0xdfdc9d,-8|15|0x005135",95,0,0,0},
	["8-0-0-4"]={"训练-未完成",{96,486,623,660},"0|0|0xd7ad14-0x111111,-6|2|0xcda514-0x111111,-6|8|0xc9a214-0x111111,91|5|0x957915-0x111111,78|7|0xbc9815-0x111111,79|12|0xd0a814-0x111111,111|15|0xa08215-0x111111,121|15|0x937815-0x111111",95,0,0,0},
	["8-0-0-5"]={"选择-人物图标",{4,4,145,135},"0|0|0x201e28,-4|15|0x1d1a27,-1|25|0xa4815f,19|19|0xdec29c,5|16|0x907760,64|-22|0x21e402,71|3|0x20ec00,44|54|0x251e0c",95,0,0,0},
	["8-0-0-6"]={"战旗-城堡界面",{375,649,415,699},"0|0|0x91401f,3|7|0x9a4017,2|13|0xbf6a1f,0|18|0xca7a3d,4|18|0xb6720d,5|3|0x913716",95,0,0,0},
	["8-0-0-7"]={"迁城-世界地图",{35,804,90,838},"0|0|0xd04d35-0x111111,4|2|0xd95037-0x111111,-1|4|0xba442f-0x111111,10|5|0xe8523b-0x111111,8|-1|0xe15239-0x111111",95,0,0,0},
	["8-0-0-8"]={"邮件-城堡地图",{6,6,90,87},"0|0|0x9f9a98-0x011111,2|0|0x9f9a98-0x011111,3|-6|0xb1acab-0x011111,-19|-3|0xa7a1a0-0x011111,-24|2|0x9c9695-0x011111,-16|9|0x898585-0x011111,-3|15|0xa7a5a6-0x011111,4|22|0x736c6a-0x011111,-4|5|0x2a507d-0x011111",95,0,0,0},
	["8-0-0-9"]={"骑宠台阶-城堡地图",{536,722,565,747},"0|0|0xfdcf19-0x011111,-6|2|0xcf9c28-0x011111,-6|3|0xd19019-0x011111,2|7|0xda9a27-0x011111,-2|7|0xe3ab41-0x011111,-7|1|0x94660c-0x011111,0|2|0xeaca19-0x011111,-4|3|0xe6b01c-0x011111",95,0,0,0},
	["8-0-0-10"]={"点击英雄升级-地下城",{424,938,593,1102},"0|0|0x6b6e79-0x001111,7|-2|0x6e707a-0x001111,0|-37|0xcfe2f1-0x001111,-27|-7|0xa9b2bc-0x001111,-24|10|0x868f9e-0x001111,-1|10|0x747c89-0x001111,44|10|0xb3e140-0x001111,58|8|0x60972b-0x001111,43|32|0xa4d33d-0x001111,50|27|0x101311-0x001111,46|21|0xa3d33d-0x001111",95,0,0,0},
	["8-0-0-11"]={"升级界面-地下城",{41,1150,127,1245},"0|0|0xcdcdc5-0x001111,-13|9|0xede8de-0x001111,-8|16|0x4e473b-0x001111,2|21|0x423422-0x001111,10|30|0x524a3e-0x001111,14|32|0x625b4c-0x001111,14|10|0x625542-0x001111,11|-7|0xb5ab9c-0x001111,15|-12|0xcfc6b7-0x001111",95,0,0,0},
	["8-0-0-12"]={"选项升级界面-地下城",{417,1118,647,1219},"0|0|0xbeea0a-0x001111,-1|5|0xb2e118-0x001111,-8|0|0x9ecb29-0x001111,19|1|0xabd81c-0x001111,17|2|0x9fcd28-0x001111,18|-8|0x87b239-0x001111,21|-5|0x507b6a-0x001111,26|5|0xc0ec08-0x001111,31|-3|0x507a6c-0x001111,16|-12|0xbee909-0x001111,23|-12|0xbee909-0x001111,28|-12|0xbee909-0x001111,41|0|0x25559a-0x001111,-5|12|0x094b83-0x001111,-29|-6|0x244e94-0x001111,-49|6|0x214f91-0x001111",95,0,0,0},
	["8-0-0-13"]={"还没到时间-造兵",{296,478,327,519},"0|0|0xc944fd,3|0|0xbf3dfd,-1|4|0xf47fff,-3|-1|0xd457fe,3|5|0xce50fa",95,0,0,0},  --显示钻石
	["8-0-0-14"]={"锤子-建筑",{25,363,86,397},"0|0|0xada891-0x011111,-13|7|0xc2bb9a-0x011111,-1|7|0x9e977c-0x011111,10|8|0xb5b19b-0x011111,6|-2|0xa9a28b-0x011111,-10|0|0x999278-0x011111",95,0,0,0},
	["8-0-0-15"]={"帐篷-建筑",{550,786,625,889},"0|0|0x98e429-0x011111,24|-2|0x75c71a-0x011111,11|-13|0xb1f93b-0x011111,2|-37|0x9fea32-0x011111,14|-39|0xb8f151-0x011111,25|-32|0x74c619-0x011111,1|-24|0x619b02-0x011111",95,0,0,0},
	["8-0-0-16"]={"帐篷升级-建筑",{501,1190,585,1240},"0|0|0xb6c34b-0x011111,-2|1|0xf3e63e-0x011111,-2|4|0xcacd46-0x011111,-2|9|0xcacc44-0x011111,10|3|0xb4c149-0x011111,10|0|0xebe140-0x011111,18|3|0xdcd842-0x011111,22|7|0xe4dc40-0x011111",95,0,0,0},
	["8-0-0-17"]={"挖金矿-挖矿",{483,1212,541,1239},"0|0|0xa9c817,-1|4|0xecf104,19|6|0xfbfc01,32|7|0xadc40f,32|12|0xd1e00a",95,0,0,0},--挖矿派兵
	["8-0-0-18"]={"没有队列-挖矿",{222,549,550,740},"0|0|0xd8ae14-0x111111,3|8|0xd7ad14-0x111111,5|37|0xd0a814-0x111111,9|45|0xcfa714-0x111111",95,0,0,0},--挖矿队列已满
	["8-0-0-19"]={"4个队列-挖矿",{193,341,255,389},"0|0|0x90d0d3-0x111111,0|15|0x88c7ca-0x111111,12|7|0x639ba1-0x111111,15|15|0x7db9be-0x111111",95,0,0,0},--挖矿队列已满
	["8-0-0-20"]={"骷髅头自动-挖矿",{596,987,707,1102},"0|0|0xe0cc9e,12|0|0xb3956a,18|11|0x342f2e,15|18|0x2f2c27,8|25|0x54493b,-1|24|0x84755b",95,0,0,0},--挖矿
	["8-0-0-21"]={"自动-挖矿",{53,1044,162,1153},"0|0|0xe7a759,3|4|0xe69f52,3|10|0x7f461e,-18|-29|0x8c4d20,-27|-7|0x3a3e46",95,0,0,0},--挖矿
	["8-0-0-22"]={"自动-挖矿",{219,717,484,828},"0|0|0xebeca6,5|7|0xe9e9a4,29|2|0xfff8af,28|9|0xb0c389,44|8|0xfbf5ad,49|9|0xc7d394",95,0,0,0},--挖矿
	["8-0-0"]={{20, 148, 553, 1004},"0|0|0xffffff,0|8|0xffffff",95,0,0,0}, --找到金矿
};
GlobalX,GlobalY = -1,-1;
gCountSoldier = 0
function isFindCoordinate(selectIndex)
	local tmpArray = Array[selectIndex]
	GlobalX,GlobalY = -1,-1;
	GlobalX,GlobalY = findColor(tmpArray[2],tmpArray[3],tmpArray[4],tmpArray[5],tmpArray[6],tmpArray[7])
	if GlobalX ~= -1 then
		return true
	else
		return false	
	end
end

function ClickOn(delay,x,y)
	if x > -1 then
		mSleep(delay);
		touchDown(1,x,y);
		mSleep(delay)
		touchUp(1,x,y);
        mSleep(delay)
	end
end

function SlideScreen(delay,point_1,point_2)
	touchDown(1,point_1[1],point_1[2])
	mSleep(delay)
	touchMove(1,point_2[1],point_2[2])
	mSleep(delay)
	touchUp(1,point_2[1],point_2[2])
    mSleep(delay)
end

function SendMessage(delay,content,sendTable)
	print("发送消息")
	toast("发送消息")
	inputText("#CLEAR#")
	inputText(content)
	ClickOn(delay,sendTable[1],sendTable[2])
	mSleep(delay)
end

function GoCastle()  -- 进入城堡
	local flag = true
	while flag do   -- 一直到找到世界标志为止
		if isFindCoordinate("8-0-0-7") then
			ClickOn(200,48,1225) --点击圆球进入城堡
			toast("进入城堡")
			flag = false
			mSleep(50)
		end
	end
end
function GoGlobal()  -- 进入世界地图
	quit()
	for i = 1,6 do
		if isFindCoordinate("8-0-0-7") then
			print("进入世界图-for")
			toast("进入世界图-for")
			mSleep(1000)
			break
		else
			ClickOn(200,48,1225) --点击圆球进入世界地图
			print("进入世界图-else")
			toast("进入世界图-else")
			mSleep(1000)
		end
	end
    mSleep(500)
end

function Display(delay,content)
    if not delay then
        mSleep(1000)
    else
        mSleep(delay)
        toast(content)
        mSleep(delay)
    end
end

function BeatMonster(times)
	print("打怪")
	toast("打怪")
    for i = 1,times do
        --2秒
		local count = 0;
		while count < 80 do
			count = count + 1
			ClickOn(20,512,390)  -- 点击打怪
		end
		mSleep(100)
		--ClickOn(100,664,553)  --进入下一关
	end
end

function LianMengZhuanXian()
	ClickOn(200,660,1230)
	mSleep(3000)
	ClickOn(200,270,900)
	mSleep(3000)
	local count = 0
	while count < 30 do
		ClickOn(300,350,1100)
		count = count + 1
	end
end

function MakeSoldier()
	print("造兵")
	toast("造兵")
	mSleep(500)
	SlideScreen(500,{650,530},{300,530})  --最右端
	SlideScreen(500,{440,530},{510,530})  --进到兵营
	mSleep(2000)
	ClickOn(300,240,355)    --可能已经完成了造兵，点击出货
	mSleep(500)
	if isFindCoordinate("8-0-0-13") then
		ClickOn(200,500,500)
		print("时间未到！")
		toast("时间未到！")
		return 
	end
	ClickOn(100,240,355)    --点击兵营
	mSleep(500)
	ClickOn(300,385,435)    --点击兵营训练
	mSleep(500)
	--ClickOn(200,600,900)    --点击选择的兵种
	ClickOn(200,500,1210)   --点击兵种训练
	mSleep(500)
end

function GoToRuin()
	print("进入地下城")
	toast("进入地下城")
	SlideScreen(500,{650,530},{100,530})  --最右端
	mSleep(500)
	SlideScreen(800,{440,530},{490,530})  --找到地下城
	mSleep(500)
	SlideScreen(500,{440,600},{440,500})  --最下端
	mSleep(500)
	ClickOn(800,340,955)  --点击显示地下城
	mSleep(500)
	ClickOn(500,445,900)  --点击进入地下城
	mSleep(2000)
	ClickOn(500,512,390)  -- 点击打怪
end

function LoadCastle()
	print("加载城堡")
	toast("加载城堡")
	for i = 1,2000 do
		if isFindCoordinate("8-0-0-9") then  --判断城堡内部画面加载完毕
			Display(50,"城堡加载完毕")
			mSleep(50)
			break
		end
		mSleep(50)
	end
	mSleep(500)
end

function LevelHero()
	print("英雄升级")
	toast("英雄升级")
	if isFindCoordinate("8-0-0-10") then 
		ClickOn(200,GlobalX,GlobalY) --点击打怪界面的升级英雄
		mSleep(1000)
		if isFindCoordinate("8-0-0-11") then
			--升级
			for _,x in pairs({180,300,420,540}) do
				print(x)
				ClickOn(200,x,1200)
				if isFindCoordinate("8-0-0-12") then
					ClickOn(200,GlobalX,GlobalY)
					print("升级坐标(" .. tostring(x) .. "," ..  "1200)")
					break
				elseif isFindCoordinate("8-0-0-8") then
					ClickOn(200,GlobalX,GlobalY)
				end
			end
		else
			print("没有进入到选择升级选项")
			toast("没有进入到选择升级选项")
		end
	else
		print("没有找到升级图标或者正在升级状态")
		toast("没有找到升级图标或者正在升级状态")
	end
	mSleep(800)
	if isFindCoordinate("8-0-0-8") then
		ClickOn(200,GlobalX,GlobalY)
	end
end

function quit()
	print("退出")
	toast("退出")
	quitFlag = true
	while quitFlag do
		if isFindCoordinate("8-0-0-8") then  --判断是否进入了邮件系统
			ClickOn(200,GlobalX,GlobalY)
			mSleep(500)
		else
			quitFlag = false
		end
	end
	mSleep(500)
end

function MakeBuild() --造建筑
	--[[for i = 1,5 do 
		if isFindCoordinate("8-0-0-14") then --有锤子
			SlideScreen(500,{300,530},{650,530})  --最左端
			SlideScreen(500,{440,1000},{440,800})  --进到兵营
			ClickOn(200,505,787) --左下角帐篷
			mSleep(500)
			ClickOn(200,575,825)  --点击升级
			mSleep(500)
			if isFindCoordinate("8-0-0-16") then
				ClickOn(200,GlobalX,GlobalY)
				print("造建筑")	
			end
		end
		mSleep(300)
	end]]
end

function isFindCoordinateTable(var)
	local tmpArray = Array[var]
	pointTable = findColors(tmpArray[1],tmpArray[2],tmpArray[3], tmpArray[4], tmpArray[5], tmpArray[6])
	if #pointTable ~= 0 then
		toast(#pointTable)
		return pointTable
	end
end

function SearchMine(mode)
	if mode == 0 then  --坐标挖矿
		for x = 1,50 do
			for y = 1,50 do
				print(string.format("(%s,%s)",x,y))
				local len = math.sqrt(math.pow(x,2) + math.pow(y,2))
				print(string.format("长度为:%.2f",len))
				if len <= 50 then 
					GoToXY(275+x,615+y)
					mSleep(1000)
					tempTable = isFindCoordinateTable("8-0-0")
					if tempTable then
						print(#tempTable)
						for index,var in pairs(tempTable) do
							if var.x < 490 then
								print(string.format("%s,%s,%s",index,var.x,var.y))
								ClickOn(1000,var.x,var.y-2)
								ClickOn(1000,var.x+140,var.y-20)
								for i = 1,30 do
									print(string.format("2:%s",i))
									if isFindCoordinate("8-0-0-18") or isFindCoordinate("8-0-0-19") then  --队列已满
										toast("队列已满，休眠1分钟")
										print("队列已满，休眠1分钟")
										ClickOn(500,500,1112)
										mSleep(60000)
									else
										break
									end
								end
								if isFindCoordinate("8-0-0-17") then
									ClickOn(200,GlobalX,GlobalY)
								else
									ClickOn(200,50,45)
								end
								ClickOn(500,500,1112)
							end
						end
					end
				end
			end
		end
	elseif mode == 1 then
		mSleep(200)
		if isFindCoordinate("8-0-0-20") then
			ClickOn(100,GlobalX,GlobalY)
			mSleep(200)
			if isFindCoordinate("8-0-0-21") then
				ClickOn(100,GlobalX,GlobalY)
			--ClickOn(100,115,1100)
				mSleep(200)
				if isFindCoordinate("8-0-0-22") then
					ClickOn(100,GlobalX,GlobalY)
				end
			end
		end
	end
	print("SearchMine end")
end

function GoToXY(x,y)
	GoGlobal()
	ClickOn(50,360,980)
	ClickOn(20,187,1081)
	print(string.format("(%s,%s)",x,y))
	toast(string.format("(%s,%s)",x,y))
	ClickOn(2,240,600)
	inputText(string.format("%s#ENTER#",x))
	ClickOn(2,500,600)
	inputText(string.format("%s#ENTER#",y))
	ClickOn(10,360,760)
end
